package com.emall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.common.constant.AuthConstant;
import com.common.utils.R;
import com.emall.auth.feign.MemberFeignService;
import com.emall.auth.vo.MemberRespVo;
import com.emall.auth.vo.UserLoginVo;
import com.emall.auth.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YuanShijia
 * @ClassName IndexController.java
 * @Description
 * @createTime 2020年05月21日 10:55:00
 */
@Controller
public class RegController {

    // sms

    @Autowired
    MemberFeignService memberFeignService;

    @PostMapping("/regist")
    public String regist(@Valid UserRegistVo vo, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()){
            /**
             * map(fieldError -> {
             *                 String field = fieldError.getField();
             *                 String defaultMessage = fieldError.getDefaultMessage();
             *                 errors.put(field, defaultMessage);
             *             })
             */
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(fieldError -> {
                return fieldError.getField();
            }, fieldError -> {
                return fieldError.getDefaultMessage();
            }));


            redirectAttributes.addFlashAttribute("errors", errors);
            // 校验出错
            return "redirect:http://127.0.0.1:20000/reg.html";
        }
        // 远程服务注册
            // 校验验证码
        R r = memberFeignService.regist(vo);
        if (r.getCode() == 0){

            return "redirect:http://127.0.0.1:20000/login.html";
        }else {
            HashMap<String, String> errors = new HashMap<>();
            errors.put("message", r.getData("message", new TypeReference<String>(){}));
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://127.0.0.1:20000/reg.html";
        }
    }

    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session){

        // feign
        R login = memberFeignService.login(vo);
        if (login.getCode() == 0){
            MemberRespVo data = login.getData("data", new TypeReference<MemberRespVo>() {});
            session.setAttribute(AuthConstant.LOGIN_USER,data);
            return "redirect:http://127.0.0.1:10000/index.html";
        }else {
            HashMap<String, String> errors = new HashMap<>();
            errors.put("message", login.getData("message", new TypeReference<>(){}));
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://127.0.0.1:20000/login.html";
        }


    }

}
